// pages/info/info.js
import { showModal,login,checkSession} from '../../utils/asyncwx.js'
const http = require('../../utils/http.js');
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: null,
    userInfo: {},
    disabled:false,
    phoneNum: '',
    tname: '',
  },
  toback: function () {
    //返回
    wx.navigateBack({
      delta: 1
    });

  },// end of toback
  async get_data() {

   
    let url = 'client/detail';
    let data = {
      uid: this.data.uid,
    }
    let res= await http.post(url,data)

    if (res.client.name == '') {
      //没有名字信息
     

    } else {
      /* wx.setStorage({
         key: 'address',
         data: res,         
       })*/
       wx.setStorageSync('tname',res.client.name) //保存用户的姓名
       this.setData({
        
       
        tname: res.client.name,
        phoneNum: res.client.tel,
        
      })
    }


  },
  addnameInfo: function (e) {
    //姓名输入
    let tname = e.detail.value
    this.setData({
      tname: tname
    })
   
  },
  inputPhoneNum: function (e) {
    // 手机号码输入
  
    let phoneNum = e.detail.value
    this.setData({
      phoneNum: phoneNum
    })
   
    
  },
  checktname (tname) {
    if (tname.length>1){
      return true
    } else {
       showModal({content:'请输入姓名!',showCancel:false})
       return false
    }
  },
   checkPhoneNum (phoneNum) {
    //检查手机格式是否正确
    let str = /^1[3-9][0-9]\d{8}$/  // if (!value.tel.match(/^1[3-9][0-9]\d{8}$/)) {
    if (str.test(phoneNum)) {
      return true
    } else {
       showModal({content:'手机号格式不正确!',showCancel:false}) 
       return false
    }
  },
  async gettoken(){
    //从后台获取token
    let url = 'wx/login/'
    let data = {
      uid: this.data.uid,     
    }
  
    let res= await http.post(url,data)
    if(res!=null){
      wx.setStorageSync('token',res)
    }
    

  },
  async dosubmit() {
    //提交数据
    let uid = this.data.uid//+" "+ pdname
    let phoneNum = this.data.phoneNum
    let tname = this.data.tname
   
    let url = 'client/edit';
    
    let data = {
      id: uid,
      tel: phoneNum,
      name: tname
    }
    let res= await http.post(url,data)
   
    if (res){
     
        wx.setStorageSync('role',res.role)
        //跳转到个人中心
        await showModal({content:'修改信息成功!',showCancel:false})
        wx.switchTab({
          url: '/pages/mine/index'
        })
       
     
    }
  }, 
  async submitForm(e) {
    //提交表单
     //1 。判断缓存中有没有token
     const token=wx.getStorageSync("token")
   
     // 2.判断
     if(!token){        
       wx.navigateTo( //跳转到授权页面
         {url:'/pages/login/index'}
       )
       return false
     } 
   
    let phoneNum = this.data.phoneNum
    let tname = this.data.tname

    
    let checkedNum = this.checkPhoneNum(phoneNum)
    let checktname=this.checktname(tname)
    
    if( (checkedNum)&& (checktname)) {
        
     
        this.dosubmit()
        
      }
    

  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    const userInfo=wx.getStorageSync("userInfo")
    const uid=wx.getStorageSync("uid")
    this.setData({
      userInfo,
      uid
    })
    this.gettoken()
    this.get_data() 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})